package leetcode_201_300;

import node.TreeNode;

import java.util.ArrayList;
import java.util.List;

public class LeeCode_230 {
    public static void main(String[] args) {
        System.out.println(kthSmallest(new TreeNode(3, new TreeNode(1, null, new TreeNode(2)), new TreeNode(4)), 1));
    }
    private static int kthSmallest(TreeNode root, int k) {
        List<Integer> list = new ArrayList<>();
        dfs(list, root, k);
        return list.get(k - 1);
    }
    private static void dfs(List<Integer> list, TreeNode node, int k){
        if (node == null){
            return;
        }
        if (list.size() == k){
            return;
        }
        dfs(list, node.left, k);
        if (list.size() == k){
            return;
        }
        list.add(node.val);
        if (list.size() == k){
            return;
        }
        dfs(list, node.right, k);
    }
}
